package com.yang.crawler.event;


import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.EventBus;
import com.yang.crawler.event.factory.EventHandlerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EventCenter {
    @Autowired
    private EventBus eventBus;

    @Autowired
    private AsyncEventBus asyncEventBus;

    public void postEvent(IEvent iEvent) {
        String eventName = iEvent.getEventName();
        IEventHandler iEventHandler = EventHandlerFactory.getEventHandler(eventName);
        eventBus.register(iEventHandler);
        eventBus.post(iEvent);
    }

    public void asyncPostEvent(IEvent iEvent) {
        String eventName = iEvent.getEventName();
        IEventHandler iEventHandler = EventHandlerFactory.getEventHandler(eventName);
        asyncEventBus.register(iEventHandler);
        asyncEventBus.post(iEvent);
    }
}

